﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Message 的摘要说明
/// </summary>
public class Message
{
    public const string StrSent = "sent";
    public const string StrRecv = "recv";
    public const string StrRead = "read";
    public const string StrDel = "delmes";
    public int Guid;
    public int FromGuid;
    public int[] ToGuids;
    public string Title;
    public string Attachment;
    public string Body;
    //public string Recvers;
    public int RecversNum;
    public DateTime CreateTime;
    public string ToGuidsStr;
	public Message(int g)
	{
        this.Guid = g;
        string sql = "select * from Message where Guid="+g.ToString();
        DataRow dr = Fun.CreateDr(sql);
        this.FromGuid = Convert.ToInt32(dr["FromGuid"]);
        this.Title = dr["Title"].ToString().Trim();
        this.Attachment = dr["Attachment"].ToString().Trim();
        //this.Recvers = dr["Recvers"].ToString().Trim();
        this.Body = dr["Body"].ToString();
        this.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
        sql = "select GuidOne from Relationship where (Relationship='"+StrRecv+"' or Relationship='"+StrRead+"' or Relationship='"+StrDel+"') and GuidTwo="+this.Guid.ToString();
        DataTable dt = Fun.CreateDt(sql);
        this.ToGuids = new int[dt.Rows.Count];
        this.ToGuidsStr = "";
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            this.ToGuids[i] = Convert.ToInt32(dt.Rows[i][0]);
            this.ToGuidsStr = this.ToGuidsStr + " " + this.ToGuids[i].ToString();
        }
        this.ToGuidsStr = this.ToGuidsStr.Trim();
        this.RecversNum = this.ToGuids.Length;
	}

    public string SenderName(bool usePaperName)
    {
        string sql = "";
        if (usePaperName)
        {
            sql = "select UserType as n from Users where Guid="+this.FromGuid.ToString();
            string ut = Fun.GetField(sql).ToString().Trim();
            if (ut == LwUser.StudentType)
            {
                sql = "select PaperName as n from Student where UserGuid=" + this.FromGuid.ToString();
                return Fun.GetField(sql).ToString().Trim();
            }
            else if(ut==LwUser.AdminType)
            {
            }
            return "评审专家";
        }
        else
        {
            sql = "select RealName as n from Users where Guid="+this.FromGuid.ToString();
        }
        return Fun.GetField(sql).ToString().Trim();
    }

    public string SenderName()
    {
        return SenderName(false);
    }

    public string SenderName( int recverGuid)
    {
        if (Administrator.IsAdmin(recverGuid))
        {
            return this.SenderName();
        }
        if (Team.IsTeam(recverGuid))
        {
            return this.SenderName();
        }
        if (Doctor.IsDoctor(recverGuid))
        {
            if (Doctor.DoctorHasStudent(recverGuid, this.FromGuid) || Administrator.IsAdmin(this.FromGuid) || Team.IsTeam(this.FromGuid) || Doctor.IsDoctor(this.FromGuid))
            {
                return this.SenderName();
            }
            else
            {
                return this.SenderName(true);
            }
        }
        if (Student.IsStudent(recverGuid))
        {
            if (Doctor.DoctorHasStudent(this.FromGuid, recverGuid) || Administrator.IsAdmin(this.FromGuid) || Team.IsTeam(this.FromGuid) || Doctor.IsDoctor(this.FromGuid))
            {
                return this.SenderName();
            }
            else
            {
                return this.SenderName(true);
            }
        }
        return "";
    }

    public bool ReadYeat(int g)
    {
        return Relationship.ExistRelationship(g,StrRead, this.Guid);
    }

    public static int Send(int f, string ts,string tle,string bod)
    {
        string[] ss = ts.Split(',');
        int g = Fun.Guid();
        string sql = "insert into Message(Guid,FromGuid,Title,Body) values("+g.ToString()+","+f.ToString()+",'" + tle.Replace("'", "''") + "','" + bod.Replace("'", "''") + "')";
        Fun.Execute(sql);
        Relationship.AddRelationship(f, StrSent, g);
        string rs = "";
        foreach (string s in ss)
        {
            if (!Fun.IsNumeric(s))
            {
                continue;
            }
            rs = rs + "," + s;
            Relationship.AddRelationship(Convert.ToInt32(s), StrRecv, g);
        }
        sql = "select RealName from Users where Guid in ("+rs.Trim(',')+")";
        DataTable dt = Fun.CreateDt(sql);
        rs = "";
        foreach (DataRow dr in dt.Rows)
        {
            rs = rs + "，" + dr[0].ToString().Trim();
        }
        sql = "update Message set Recvers='"+Fun.L(rs.Trim(','))+"' where Guid="+g.ToString();
        Fun.Execute(sql);
        return g;
    }

    public static int Send(int f, string ts, string tle, string bod, string attachmentName)
    {
        string[] ss = ts.Split(',');
        int g = Fun.Guid();
        string sql = "insert into Message(Guid,FromGuid,Title,Body,Attachment) values(" + g.ToString() + "," + f.ToString() + ",'" + tle.Replace("'", "''") + "','" + bod.Replace("'", "''") + "','"+Fun.L(attachmentName)+"')";
        Fun.Execute(sql);
        Relationship.AddRelationship(f, StrSent, g);
        string rs = "";
        foreach (string s in ss)
        {
            if (!Fun.IsNumeric(s))
            {
                continue;
            }
            rs = rs + "," + s;
            Relationship.AddRelationship(Convert.ToInt32(s), StrRecv, g);
        }
        sql = "select RealName from Users where Guid in (" + rs.Trim(',') + ")";
        DataTable dt = Fun.CreateDt(sql);
        rs = "";
        foreach (DataRow dr in dt.Rows)
        {
            rs = rs + "，" + dr[0].ToString().Trim();
        }
        sql = "update Message set Recvers='" + Fun.L(rs.Trim(',')) + "' where Guid=" + g.ToString();
        Fun.Execute(sql);
        return g;
    }

    public static int Send(int f, int t, string tle, string bod,string recvers)
    {
        int g = Fun.Guid();
        string sql = "insert into Message(Guid,FromGuid,Title,Body,Recvers) values("+g.ToString()+"," + f.ToString() + ",'" + tle.Replace("'", "''") + "','" + bod.Replace("'", "''") + "','"+Fun.L(recvers)+"')";
        Relationship.AddRelationship(f, StrSent, g);
        Relationship.AddRelationship(t, StrRecv, g);
        return g;
    }

    public static int Send(int f, int t, string tle, string bod, string recvers,string attachmentName)
    {
        int g = Fun.Guid();
        string sql = "insert into Message(Guid,FromGuid,Title,Body,Recvers,Attachment) values(" + g.ToString() + "," + f.ToString() + ",'" + tle.Replace("'", "''") + "','" + bod.Replace("'", "''") + "','" + Fun.L(recvers) + "','" + Fun.L(attachmentName) + "')";
        Relationship.AddRelationship(f, StrSent, g);
        Relationship.AddRelationship(t, StrRecv, g);
        return g;
    }

    public static void Delete(int g)
    {
        Relationship.Delete(g);
        string sql = "delete from Message where Guid="+g.ToString();
        Fun.Execute(sql);
    }

    public static string GetSenderName(int mesGuid, int recverGuid)
    {
        Message mes=new Message(mesGuid);
        if (Administrator.IsAdmin(recverGuid))
        {
            return mes.SenderName();
        }
        if (Team.IsTeam(recverGuid))
        {
            return mes.SenderName();
        }
        if (Doctor.IsDoctor(recverGuid))
        {
            if (Doctor.DoctorHasStudent(recverGuid, mes.FromGuid) || Administrator.IsAdmin(mes.FromGuid) || Team.IsTeam(mes.FromGuid) || Doctor.IsDoctor(mes.FromGuid))
            {
                return mes.SenderName();
            }
            else
            {
                return mes.SenderName(true);
            }
        }
        if (Student.IsStudent(recverGuid))
        {
            if (Doctor.DoctorHasStudent(mes.FromGuid, recverGuid) || Administrator.IsAdmin(mes.FromGuid) || Team.IsTeam(mes.FromGuid) || Doctor.IsDoctor(mes.FromGuid))
            {
                return mes.SenderName();
            }
            else
            {
                return mes.SenderName(true);
            }
        }
        return "";
    }
}
